//
//  VVTabBarControllerHandler.m
//  VOVA
//
//  Created by fwzhou on 2020/3/27.
//  Copyright © 2020 iOS. All rights reserved.
//

#import "LBTabBarControllerHandler.h"
#import "LBMainPageViewController.h"
#import "LBViewController.h"
#import "LBTabBarNormalRedPointView.h"
#import "LBTabBarNumRedPointView.h"
#import "LBTabBarRedPointManager.h"
#import "LBTabBarConfigModel.h"
#import "LBTabBarModel.h"

#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height


@interface LBTabBarControllerHandler ()

@property(nonatomic, weak) LBTabBarController *tabBarController;

@end

@implementation LBTabBarControllerHandler

+ (LBTabBarController *)configTabBarController
{
    LBMainPageViewController *mainPage = [[LBMainPageViewController alloc] init];
    LBMainPageViewController *category = [[LBMainPageViewController alloc] init];
    LBMainPageViewController *cart = [[LBMainPageViewController alloc] init];
    LBViewController *account = [[LBViewController alloc] init];
    NSArray *vcs = @[
        mainPage,
        category,
        cart,
        account,
    ];
    NSMutableArray *navs = [NSMutableArray array];
    [vcs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:obj];
        [navs addObject:nav];
    }];
    
    
    NSDictionary *attributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:10],
        NSForegroundColorAttributeName: [UIColor cyanColor]
    };
    NSDictionary *selectAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:11],
        NSForegroundColorAttributeName: [UIColor greenColor],
    };
    
    LBTabBarModel *mainModel = [LBTabBarModel imageTypeImage:VVTABBARFILE(@"tabbar_home_normal") selectedImage:VVTABBARFILE(@"tabbar_home_selected")];
    mainModel.edgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    mainModel.selectedEdgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    mainModel.size = CGSizeMake(20, 20);
    mainModel.selectedSize = CGSizeMake(20, 20);
    mainModel.attributedString = [[NSAttributedString alloc] initWithString:@"首页" attributes:attributes];
    mainModel.selectedAttributedString = [[NSAttributedString alloc] initWithString:@"首页" attributes:selectAttributes];
    
    
    LBTabBarModel *categoryModel = [LBTabBarModel imageTypeImage:VVTABBARFILE(@"tabbar_category_normal") selectedImage:VVTABBARFILE(@"tabbar_category_selected")];
    categoryModel.edgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    categoryModel.selectedEdgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    categoryModel.size = CGSizeMake(20, 20);
    categoryModel.selectedSize = CGSizeMake(20, 20);
    categoryModel.attributedString = [[NSAttributedString alloc] initWithString:@"分类" attributes:attributes];
    categoryModel.selectedAttributedString = [[NSAttributedString alloc] initWithString:@"分类" attributes:selectAttributes];
    
    LBMiddleTabBarModel *tabBarModel = [LBMiddleTabBarModel imageTypeImage:VVTABBARFILE(@"freebies_placeholder") selectedImage:VVTABBARFILE(@"freebies_alert_bg")];
    tabBarModel.convexHeight = 20;
    tabBarModel.width = 40;
    tabBarModel.size = CGSizeMake(20, 20);
    tabBarModel.selectedSize = CGSizeMake(20, 20);
    
    LBTabBarModel *cartModel = [LBTabBarModel imageTypeImage:VVTABBARFILE(@"tabbar_cart_normal") selectedImage:VVTABBARFILE(@"tabbar_cart_selected")];
    cartModel.edgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    cartModel.selectedEdgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    cartModel.size = CGSizeMake(20, 20);
    cartModel.selectedSize = CGSizeMake(20, 20);
    cartModel.attributedString = [[NSAttributedString alloc] initWithString:@"购物车" attributes:attributes];
    cartModel.selectedAttributedString = [[NSAttributedString alloc] initWithString:@"购物车" attributes:selectAttributes];
    
    LBTabBarModel *accountModel = [LBTabBarModel imageTypeImage:VVTABBARFILE(@"tabbar_me_normal") selectedImage:VVTABBARFILE(@"tabbar_me_selected")];
    accountModel.edgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    accountModel.selectedEdgeInsets = UIEdgeInsetsMake(8, -1, 7, 0);
    accountModel.size = CGSizeMake(20, 20);
    accountModel.selectedSize = CGSizeMake(20, 20);
    accountModel.attributedString = [[NSAttributedString alloc] initWithString:@"我的" attributes:attributes];
    accountModel.selectedAttributedString = [[NSAttributedString alloc] initWithString:@"我的" attributes:selectAttributes];
    
    NSArray<LBTabBarModel *> *tabBarModels = @[
        mainModel,
        categoryModel,
        cartModel,
        accountModel,
    ];
    
    LBMiddleTabBarModel *middleModel;
    middleModel = [LBMiddleTabBarModel imageTypeImage:VVTABBARFILE(@"freebies_placeholder") selectedImage:VVTABBARFILE(@"freebies_alert_bg")];
    middleModel.convexHeight = 20;
    middleModel.edgeInsets = UIEdgeInsetsMake(5, -1, 7, 0);
    middleModel.size = CGSizeMake(50, 50);
    middleModel.width = SCREEN_W / 5;
    
    LBTabBarControllerHandler *handler = [LBTabBarControllerHandler shareInstance];
    LBTabBarConfigModel *tabbarConfig = [[LBTabBarConfigModel alloc] init];
    tabbarConfig.backgroundColor = [UIColor whiteColor];
    tabbarConfig.shadowImageColor = [UIColor greenColor];
    LBTabBarController *tabBarVC = [[LBTabBarController alloc] initWithViewControllers:navs tabBarModels:tabBarModels middleTabBarModel:middleModel tabBarConfigModel:tabbarConfig];
    tabBarVC.middleDelegate = handler;
    
    [[LBTabBarRedPointManager manager] config:tabBarVC];
    handler.tabBarController = tabBarVC;
    
    return tabBarVC;
}

+ (instancetype)shareInstance
{
    static LBTabBarControllerHandler *handler;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handler = [[[self class] alloc] init];
    });
    return handler;
}

#pragma mark - VVTabBarMiddleSelectProtocol
- (void)tabBarMiddleSelect:(LBTabBarController *)tabBarController
{
 
}

#pragma mark - 外部调用
+ (void)selectedIndexWithClassName:(NSString *)className
{
    [self selectedIndexWithClassName:className before:nil after:nil];
}

+ (void)selectedIndexWithClassName:(NSString *)className
                            before:(nullable void(^)(UIViewController * _Nullable originVC, UIViewController * _Nullable targetVC))before
                             after:(nullable void(^)(UIViewController * _Nullable originVC, UIViewController * _Nullable targetVC))after
{
    if (!className) {
#if DEBUG
        NSAssert(NO, @"类名不能为空呀");
#endif
        return;
    }
    
    Class class = NSClassFromString(className);
    
    LBTabBarController *tabBarContoller = [LBTabBarControllerHandler shareInstance].tabBarController;
    UIViewController *originVC = [tabBarContoller getSelectedIndexFirstViewController];
    UIViewController *targetVC = [tabBarContoller getViewControllerWithClassName:class];
    
    if (before) {
        before(originVC, targetVC);
    }
    
    [tabBarContoller selectedIndexWithClassName:class];
    
    if (after) {
        after(originVC, targetVC);
    }
}

@end
